package com.itlxd.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itlxd.common.ThreadLocalUtils;
import com.itlxd.dto.OrderDTO;
import com.itlxd.dto.PageDTO;
import com.itlxd.entity.Orders;
import com.itlxd.global.SysMessages;
import com.itlxd.result.ResultVO;
import com.itlxd.service.IOrdersService;
import com.itlxd.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-12-25
 */
@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private IOrdersService orderService;

    /**
     * 下单
     *
     * @param orderDTO
     * @return
     */
    @PostMapping("/submit")
    public ResultVO<Integer> addOrderWithOrderDetail(@RequestBody OrderDTO orderDTO) {
        orderDTO.setUserId(ThreadLocalUtils.getCurrentId());
        Integer insertNum = orderService.insertOrder(orderDTO);
        return ResultVO.success(insertNum, SysMessages.INSERT_SUCCESS);
    }

    /**
     * 用户分页查询订单
     *
     * @param pageDTO
     * @return
     */
    @GetMapping("/userPage")
    public ResultVO<IPage<OrderVO>> queryOrdersByPageWithUserId(PageDTO pageDTO) {
        pageDTO.setUserId(ThreadLocalUtils.getCurrentId());
        IPage<OrderVO> orderVOIPage = orderService.listOrderVOByPage(pageDTO);
        return ResultVO.success(orderVOIPage, SysMessages.QUERY_SUCCESS);
    }

    /**
     * 后台分页查看订单
     * @param pageDTO
     * @return
     */
    @GetMapping("/page")
    public ResultVO<IPage<OrderVO>> queryOrdersByPage(PageDTO pageDTO) {
        IPage<OrderVO> orderVOIPage = orderService.listOrderVOByPage(pageDTO);
        return ResultVO.success(orderVOIPage, SysMessages.QUERY_SUCCESS);
    }

    /**
     * 修改订单状态
     * @param order
     * @return
     */
    @PutMapping
    public ResultVO<Integer> updateOrder(@RequestBody Orders order) {
        Integer updateRows = orderService.updateOrder(order);
        return ResultVO.success(updateRows, SysMessages.UPDATE_SUCCESS);
    }

    /**
     * 再来一单
     * @param order
     * @return
     */
    @PostMapping("/again")
    public ResultVO<Integer> updateOrderAgain(@RequestBody Orders order) {
        Integer row = orderService.insertOrderAgain(order.getId());
        return ResultVO.success(row, SysMessages.INSERT_SUCCESS);
    }
}

